//
//  CommentView.m
//  YZZX
//
//  Created by Easaa on 15/5/9.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "CommentView.h"

@implementation CommentView
{

    UIView          *_viewbg;
    UITextView      *_MassgeTextView;
    HXHttpRequest   *_httpRequest;
    UITextField     *CommentName;
    
}

-(id)initWithViewComment:(CGRect)frame conmmid:(NSString *)id_massge
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.id_massge = id_massge;
        [self ceartView];
        
        
    }
    
    return self;
    
}



-(void)ceartView
{
    self.backgroundColor =RGBACOLOR(0, 0, 0, 0.2);
    [self addTarget:self action:@selector(Butfantion:) forControlEvents:UIControlEventTouchUpInside];
    
    _viewbg =[[UIView alloc]initWithFrame:CGRectMake(20, 800, Dev_ScreenWidth-40, 160)];
    
    if (isIPHone4)
    {
        _viewbg =[[UIView alloc]initWithFrame:CGRectMake(20, 800, Dev_ScreenWidth-40, 160)];
        
    }
    
    
    
    [_viewbg.layer setMasksToBounds:YES];
    [_viewbg.layer setCornerRadius:0]; //设置矩圆角半径
    [_viewbg.layer setBorderWidth:0];   //边框宽度
    _viewbg.backgroundColor =[UIColor whiteColor];
    [self addSubview:_viewbg];
    
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, Dev_ScreenWidth-40, 30)];
    labelTitle.font = [UIFont boldSystemFontOfSize:14.0];  //UILabel的字体大小
    labelTitle.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    labelTitle.textColor =[UIColor blackColor];
    labelTitle.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    labelTitle.text =@"写评论";
    [_viewbg addSubview:labelTitle];
    
    
    
    _MassgeTextView =[[UITextView alloc]initWithFrame:CGRectMake(5,30, Dev_ScreenWidth-50, 60)];
    _MassgeTextView.tag =3;
    _MassgeTextView.delegate =self;
    [_MassgeTextView.layer setMasksToBounds:YES];
    [_MassgeTextView.layer setCornerRadius:3]; //设置矩圆角半径
    [_MassgeTextView.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){0, 0, 0, 0.5});
    
    [_MassgeTextView.layer setBorderColor:colorref];//边框颜色

    [_viewbg addSubview:_MassgeTextView];
    
    //焦点
    [_MassgeTextView becomeFirstResponder];
    
    

    
    
    
    UIButton *BottomConfirm = [UIButton buttonWithType:UIButtonTypeCustom];
    BottomConfirm.tag = 1 ;
    BottomConfirm.frame = CGRectMake(Dev_ScreenWidth-40-60,120,50, 30.5);
    [BottomConfirm addTarget:self action:@selector(settingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [BottomConfirm setTitle:@"发送" forState:UIControlStateNormal];
    [BottomConfirm setTitleColor:RGBCOLOR(252, 117, 148) forState:UIControlStateNormal];
    [BottomConfirm setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [BottomConfirm setBackgroundColor:RGBCOLOR(238, 238, 238)];
    BottomConfirm.titleLabel.font =[UIFont systemFontOfSize:13];
    
    [_viewbg addSubview:BottomConfirm];
    
    [BottomConfirm.layer setMasksToBounds:YES];
    [BottomConfirm.layer setCornerRadius:3]; //设置矩圆角半径
    [BottomConfirm.layer setBorderWidth:0.5];   //边框宽度
    
    
    
    
    UILabel *labelTextFild = [[UILabel alloc] initWithFrame:CGRectMake(5,120,60, 30.5)];
    labelTextFild.font = [UIFont boldSystemFontOfSize:14.0];  //UILabel的字体大小
    labelTextFild.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    labelTextFild.textColor =[UIColor blackColor];
    labelTextFild.textAlignment = NSTextAlignmentCenter;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    labelTextFild.text =@"评论名:";
    [_viewbg addSubview:labelTextFild];

    
    CommentName =[[UITextField alloc]initWithFrame:CGRectMake(65, 120, Dev_ScreenWidth-40-50-60-40, 30)];
    CommentName.tag =0;
    CommentName.delegate =self;
    CommentName.font =[UIFont systemFontOfSize:13];
    [CommentName.layer setMasksToBounds:YES];
    [CommentName.layer setCornerRadius:3]; //设置矩圆角半径
    [CommentName.layer setBorderWidth:1.0];   //边框宽度
//    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
    [CommentName.layer setBorderColor:colorref];//边框颜色
    [_viewbg addSubview:CommentName];
    
    NSUserDefaults   *userdf = [NSUserDefaults standardUserDefaults];
    
    NSString *strname = [userdf objectForKey:YZZX_Login_fullname];
    //这里需要从user里面拿出用户名来替换
    CommentName.text = strname;

    
    [UIView animateWithDuration:0.2 animations:^{
        
        
        if (isIPHone4)
        {
            _viewbg.frame = CGRectMake(20, 64, Dev_ScreenWidth-40, 160);
            
        }else
        {
            _viewbg.frame =CGRectMake(20, 64+30, Dev_ScreenWidth-40, 160);
        
        }

        
    } completion:^(BOOL finished) {
        
        
    }];
    
    
    
}




- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    
    if (theTextField == CommentName)
    {
        [CommentName resignFirstResponder];
        
    }
    
    return YES;
}




-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder]; return NO;
    }
    return YES;
}




-(void)Butfantion:(UIControl *)control
{
    [self removeFromSuperview];
}





-(void)settingButtonClicked:(UIButton *)but
{
    //取消  确定
    if(but.tag == 0)
    {
        // 销毁
        [_MassgeTextView resignFirstResponder];
        [self removeFromSuperview];
        
    }else if (but.tag == 1)
    {
        if(_MassgeTextView.text.length <=0)
        {

            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:nil
                                                                message:@"请输入内容!"
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 11;
            [AlertView show];
            
            return;
            
        }else if(CommentName.text.length <=0)
        {
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:nil
                                                                message:@"请输入评论人名字!"
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 11;
            [AlertView show];
            
            return;
        }
        
        

        
        NSDictionary  *dicpost =[NSDictionary dictionaryWithObjectsAndKeys:
                            CommentName.text,       @"name",
                            @"",                    @"email",
                          //@"www.baidu.com",       @"url"  ,
                            _MassgeTextView.text,   @"comment",
                            self.id_quote,          @"quote",
                            nil];
        
        
        
        
        //列表请求
        NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [XSZ_UICommon useridChangUaerdfLoginBoolString],             @"user_id",
                                   @"1",             @"site_id",
                                   @"articles",      @"app",
                                   @"article",       @"model",
                                   self.id_massge,   @"object_id",
                                   dicpost,          @"comment_data",
                                   nil];
        

        
        if (_httpRequest == nil)
        {
            _httpRequest = [[HXHttpRequest alloc] init];
            _httpRequest.delegate = self;
            
            
            
        }
        [_httpRequest requestForParams:dicParams andMethod:YZ_COMMENT_POST andTag:1001];//@"system.time"];//@"category.list"];
        
        
        [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        [_MassgeTextView resignFirstResponder];
        
    }
    
}


#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    [self removeFromSuperview];
    
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    
    //必须加载在这里，不能加载回调前面
    [self.delegaecomme showTableview];
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [JsonUtil jsonToDic:responseString];
    
}




- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    [self removeFromSuperview];
    
}



@end
